package com.jxt.mina.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.UUID;

import org.apache.log4j.Logger;

public class SaveMessage {
	
	private static Logger logger = Logger.getLogger(SaveMessage.class);
	
	public void saveMsg(String receiveId,String sendId,String sendTime,String content){
		Connection conn = null; 
		PreparedStatement pstmt = null;
		String sql = "insert into offline_msg(offline_id,receive_id,content,send_id,send_time,status) " +
				"values(?,?,?,?,?,?)";
		try {
			conn = DBUtil.conn();
			pstmt = conn.prepareStatement(sql);
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			pstmt.setString(1, uuid);
			pstmt.setString(2, receiveId);
			pstmt.setString(3, content);
			pstmt.setString(4, sendId);
			pstmt.setString(5, sendTime);
			pstmt.setString(6, "0");
			pstmt.executeUpdate();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				logger.error(e1);
			}
			logger.error(e);
		}finally{
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					logger.error(e);
				}
			}
			if(conn != null){
				try {
					conn.close();
				} catch (SQLException e) {
					logger.error(e);
				}
			}
			
		}
	}
	
}
